<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
            思路：这题和全排列Ⅰ类似，只不过有重复元素，最后结果需要去重而已
            终止条件：依然是path.length==nums.length 
            当然，这种写法如果案例多，会超出限制，所以需要剪枝
            */
            var permuteUnique = function (nums) {
                let res = []
                let map = new Map()
                function backTracking(path, use) {
                    if (path.length == nums.length) {
                        let str = path.join('')
                        if (!map.has(str)) {
                            map.set(str)
                            return res.push([...path])
                        }
                    }
                    for (let i = 0; i < nums.length; i++) {
                        if (!use[i]) {
                            path.push(nums[i])
                            use[i] = true
                            backTracking(path, use)
                            path.pop()
                            use[i] = false
                        }
                    }
                }
                backTracking([], [])
                return res
            }
        </script>
    </body>
</html>
